package edu.ajou.scw;

import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.Socket;

import org.json.simple.JSONObject;

public class SocketPushThread implements Runnable {

	private String mId;
	private String mType;
	private String mContent;
	
	public SocketPushThread(String id, String type, String content)
	{
		mId = id;
		mType = type;
		mContent = content;
	}

	@Override
	public void run() {
		try
    	{
    		Log.d("Push message sent through socket connection to " + mId);
    		//System.out.println("# Socket Push Notification Sent to " + ipAddr + ":" + port);
    		Socket socket = new Socket("127.0.0.1", 8787);
    		OutputStream os = socket.getOutputStream();
    		PrintWriter pw = new PrintWriter(os);
    		
    		JSONObject obj = new JSONObject();
    		obj.put("id", mId);
    		obj.put("content", mContent);
    		
    		String message = obj.toJSONString();
    		pw.write(message);
    		pw.flush();
    		
    		pw.close();
    		socket.close();
    	}
    	catch (Exception e)
    	{
    	
    	}
	}
	
	
}
